package com.oyjd.ybx.business.comment.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.oyjd.ybx.bean.po.Comment;
import com.oyjd.ybx.bean.po.GoodsInfo;
import com.oyjd.ybx.bean.vo.CommentVO;
import com.oyjd.ybx.business.comment.dao.CommentDao;
import com.oyjd.ybx.business.comment.service.CommentService;
import com.oyjd.ybx.fw.action.Page;

/**
 * 
 * 评论信息业务接口实现
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Component
@Transactional(readOnly = true)
public class CommentServiceImpl implements CommentService {
	private CommentDao dao;

	@Autowired
	public void setDao(CommentDao dao) {
		this.dao = dao;
	}

	/**
	 * 获取评论信息列表数据
	 * 
	 * @param page
	 *            页面对象
	 * @return
	 */
	public Page<CommentVO> getList(Page<CommentVO> page) {
		return dao.getList(page);
	}

	/**
	 * 根据ID获取评论信息对象信息
	 * 
	 * @param id
	 *            ID
	 * @return
	 */
	public CommentVO getCommentVO(String commentId) {
		return dao.getCommentVO(commentId);
	}

	/**
	 * 增加评论信息
	 * 
	 * @param goodsId
	 *            商品ID
	 * @param account
	 *            评论人帐号
	 * @param commentInfo
	 *            评论信息
	 * @param custIp
	 *            客户端IP
	 */
	@Transactional(readOnly = false)
	public void add(String goodsId, String account, String commentInfo, String custIp) {
		Comment comment = new Comment();
		comment.setGoodsId(goodsId);
		comment.setAccount(account);
		comment.setCommentInfo(commentInfo);
		comment.setCustIp(custIp);
		comment.setCreateTime(new Date());
		dao.create(comment);
		
		dao.executeHql("update " + GoodsInfo.class.getName() + " g set g.countComment=(select count(t.commentId) from " + Comment.class.getName() + " t where t.goodsId=g.goodsId) where g.goodsId=?", new Object[] { goodsId });
	}

	/**
	 * 删除评论信息
	 * 
	 * @param ids
	 *            多个ID
	 * @return
	 */
	@Transactional(readOnly = false)
	public void remove(String[] ids) {
		dao.remove(ids);
	}

}
